package com.luban.authserver.service.impl;


import com.luban.authserver.bean.SysUser;
import com.luban.authserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Fox
 */
@Service("userDetailsService")
public class UserServiceImpl implements UserService, UserDetailsService {

//    @Autowired
//    private UserMapper userMapper;
//    @Autowired
//    private PermissionMapper permissionMapper;

    @Override
    public SysUser getByUsername(String username) {
//        return userMapper.getByUsername(username);

        return null;
    }

//    @Autowired
//    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("admin"));
        return new User(
                username,"1111",authorities);
//
//        SysUser user = getByUsername(username);
//        List<GrantedAuthority> authorities = new ArrayList<>();
//        if(user!=null){
//            List<SysPermission> permissions = permissionMapper.selectByUserId(user.getId());
//
//            permissions.forEach(permission -> {
//                if (permission!=null && !StringUtils.isEmpty(permission.getEnname())){
//                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(permission.getUrl());
//                    authorities.add(grantedAuthority);
//                }
//            });
//            return new User(
//                    user.getUsername(),user.getPassword(),authorities);
//        }else {
//            throw new UsernameNotFoundException("用户名不存在");
//        }

    }
}
